import functions from "./functions.js";
import { getToken } from "@/utils/auth.js";
const apiUrl = `${functions.getDomain()}/uploads/singleUploadsFile`;

export default function(params) {
	
	return new Promise((relove, reject) => {

		uni.chooseImage({
			count: 1, //默认9
			sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album','camera'], //从相册选择
			success: function(res) {
				let path = res.tempFilePaths[0];
				console.log("res",res);
				uni.showLoading({
				    title: '上传中',
					mask:true
				});
				uni.uploadFile({
					url: apiUrl,
					filePath: path,
					name: 'file',
					formData: {
						type:params.type
					},
					success: (opt) => {
						let data = JSON.parse(opt.data);
						uni.hideLoading();
						
						console.log("图片返回",data);
						if(data.code == 200){
							if(params.type == 2){
								let dataObj = {
									path:path,
									imgUrl:data.data
								}
								relove(dataObj);
							}else{
								relove(data.data);
							}
						}else{
							reject();
						}
						
					},
					fail: (err) => {
						uni.hideLoading();
						reject();
					}
				});

			}
		});

	})

}
